package com.ljx.itbbs.server.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 失效发送失败队列
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple", name = "retry.enabled", havingValue = "true") // 只有当前配置文件为true才创建bean
public class ErrorMsgConfig {

    private static final String ERROR_CHANGE = "itbbs.error.exchange";
    private static final String ERROR_QUEUE = "itbbs.error.queue";
    private static final String ERROR_KEY = "error";

    @Bean
    public DirectExchange errorExchange(){
        return new DirectExchange(ERROR_CHANGE);
    }
    @Bean
    public Queue errorQueue(){
        return new Queue(ERROR_QUEUE);
    }
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(errorQueue()).to(errorExchange()).with(ERROR_KEY);
    }
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, ERROR_CHANGE, ERROR_KEY);
    }
}
